/**
 *  TextMash - simple IDE for Clojure
 *  
 *  Copyright (C) 2010 Aleksander Naszko
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package pl.olek.textmash.menu;

import java.awt.Toolkit;
import java.awt.event.ActionListener;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

/**
 * 
 * @author anaszko
 *
 */
public class MenuBuilder {

	public static int NO_KEY = -1;
	public static int SELECTED = -2;
	public static int UNSELECTED = -3;

	public static int NONE = 0;
	public static int SHIFT = java.awt.event.InputEvent.SHIFT_MASK;
	public static int CTRL = java.awt.event.InputEvent.CTRL_DOWN_MASK;
	public static int ALT = java.awt.event.InputEvent.ALT_DOWN_MASK;

	public static String truncate(String name) {
		if (name.length() > 64) {
			return name.substring(0, 10) + "..."
					+ name.substring(name.length() - 51);
		}
		return name;
	}

	public static JMenuItem item(String name, int key, boolean mod,
			ActionListener listener) {
		return item(name, key, mod, (Toolkit.getDefaultToolkit()
				.getMenuShortcutKeyMask()), listener);
	}

	public static JMenuItem item(String name, int key, ActionListener listener) {
		return item(name, key, false, 0, listener);
	}

	public static JMenuItem item(String name, int key, boolean mod, int mask,
			ActionListener listener) {
		JMenuItem menu;
		if (key > SELECTED) {
			menu = new JMenuItem(name);
		} else {
			menu = new JCheckBoxMenuItem(name);
			if (key == SELECTED) {
				menu.setSelected(true);
			}
		}

		if (key > NO_KEY) {
			menu.setAccelerator(KeyStroke.getKeyStroke(key,
					((mod ? java.awt.event.InputEvent.SHIFT_MASK : 0) | mask)));
		}
		if (listener != null) {
			menu.addActionListener(listener);
		} else {
			menu.setEnabled(false);
		}
		return menu;
	}

	public static JMenu menu(String name, int key, boolean mod,
			ActionListener listener) {
		JMenu menu = new JMenu(name);
		if (key > NO_KEY) {
			menu.setAccelerator(getStdKeyStroke(key, mod ? SHIFT : NONE));
		}
		if (listener != null) {
			menu.addActionListener(listener);
		} else {
			// menu.setEnabled(false);
		}
		return menu;
	}

	public static KeyStroke getKeyStroke(int key, int mod) {
		return KeyStroke.getKeyStroke(key, mod);
	}

	public static KeyStroke getStdKeyStroke(int key, int mod) {
		return KeyStroke.getKeyStroke(key, (mod | (Toolkit.getDefaultToolkit()
				.getMenuShortcutKeyMask())));
	}

	public static KeyStroke getKeyStroke(int key) {
		return getKeyStroke(key, NONE);
	}
}
